//获取分类数据
$.ajax({
    type: 'get',
    url: '/categories',
    success (res) {
        // console.log(res);
        var html = template('categoryTpl', { data : res });
        $('#category').html(html);
    }
})

//实现图片上传
$('#feature').on('change', function () {
    var fd = new FormData();
    fd.append('avatar', this.files[0]); 
    $.ajax({
        type: 'post',
        url: '/upload',
        data: fd,
        processData: false,
        contentType: false,
        success (res) {
            $('.thumbnail').attr('src', res[0].avatar).show();
            $('#thumbnail').val(res[0].avatar)
        }
        
    })
})

$('#addForm').on('submit', function () {
    //收集表单数据
    $.ajax({
        type: 'post',
        url: '/posts',
        data: $(this).serialize(),
        success (res) {
            //添加成功后  跳转到文章列表页
            location.href = 'posts.html';
        }
    })

    return false;
})